-- This file is part of the Fantasy Grounds Open Foundation Ruleset project. 
-- For the latest information, see http://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.


local groups = {};
local valuetable = {};
local valueobservers = {};


-- Use this function to register items in the preferences dialog.
-- 
-- valuename      A valid Lua variable name, that should be unique for each option.
--                Using the underscore character for grouping and unique identification
--                is recommended.
-- group          The label string of the group the option is placed under
-- label          The label string used for the individual option. The entry class should
--                contain a string control named "label" to house this data.
-- entryclass     A window class used to construct the entry in the preference dialog.
--                The window class must contain a field called "label" and implement the
--                "setValue" function.
-- defaultvalue   The default value for the option, used if the value is not found in the
--                registry.

function registerOption(valuename, group, label, entryclass, defaultvalue)
	local entry = {};
	
	entry.valuename = valuename;
	entry.group = group;
	entry.label = label;
	entry.entryclass = entryclass;
	entry.defaultvalue = defaultvalue;
	
	if not groups[group] then
		groups[group] = {};
	end
	table.insert(groups[group], entry);
	
	if valuetable[valuename] == nil then
		valuetable[valuename] = defaultvalue;
	end
end


function populateDialog(w)
	for k,v in pairs(groups) do
		local g = w.groups.createWindow();
		
		-- Set group label
		g.label.setValue(k);
		
		-- Populate group
		for ek, entry in pairs(v) do
			local e = g.entries.createWindowWithClass(entry.entryclass);
			e.label.setValue(entry.label);
			e.setOptionData(entry, valuetable);
		end
	end
end

function setValue(valuename, value)
	valuetable[valuename] = value;
	
	if valueobservers[valuename] then
		for k, v in pairs(valueobservers[valuename]) do
			v(valuename, value);
		end
	end
end

function getValue(valuename)
	return valuetable[valuename];
end

function registerValueObserver(valuename, observer)
	if not valueobservers[valuename] then
		valueobservers[valuename] = {};
	end
	
	table.insert(valueobservers[valuename], observer);
end

function unregisterValueObserver(valuename, observer)
	if valueobservers[valuename] then
		for k, v in pairs(valueobservers[valuename]) do
			if v == observer then
				valueobservers[valuename][k] = nil;
			end
		end
	end
end


function onInit()
	if not CampaignRegistry.preferences then
		CampaignRegistry.preferences = {};
	end

	valuetable = CampaignRegistry.preferences;
end
